package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Search extends JInternalFrame {
	DatabaseTest main;
	Connection conn;
	DefaultTableModel model;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JTextField searchsql;
	private JButton btnNewButton;
	private JTable table;


	public Search(Connection conn, DatabaseTest main) {
		super("데이타 조회", true, true, true, true);
		this.conn = conn;
		this.main = main;
		setTitle("데이타 조회");
		setBounds(873, 0, 509, 403);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getSearchsql());
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTextField getSearchsql() {
		if (searchsql == null) {
			searchsql = new JTextField();
			searchsql.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						sqlstart();
					}
				}
			});
			searchsql.setFont(new Font("굴림", Font.BOLD, 15));
			searchsql.setColumns(10);
		}
		return searchsql;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리 실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sqlstart();
				}
			});
			btnNewButton.setForeground(new Color(255, 255, 255));
			btnNewButton.setBackground(new Color(0, 0, 0));
			btnNewButton.setFont(new Font("굴림", Font.BOLD, 15));
		}
		return btnNewButton;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if(main.modify == null){
						main.openUpdate();
					}
					
					int row = table.getSelectedRow();
					String s = String.valueOf(table.getValueAt(row, 1));
					String c = (String)table.getValueAt(row, 2);
					String ee = String.valueOf(table.getValueAt(row, 3));
					
					main.modify.setData(s, c, ee);
					main.modify.setVisible(true);
					main.modify.toFront();
				}
			});
		}
		return table;
	}
	
	public void sqlstart(){
		try{
			String sql = searchsql.getText();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector colNames = new Vector(); // 벡터에 테이블항목을 저장하기위해서
			// colNames 생성.
			colNames.add("NO");
			for(int i = 1; i <= rsm.getColumnCount(); i++){
				colNames.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(colNames, 0); // (콜네임, 행의갯수).
			table.setModel(model);
			int cnt = 1;
			while(rs.next()){
				Vector r = new Vector<>();
				r.add(cnt);
				for(int i = 1; i <= rsm.getColumnCount(); i++){
					r.add(rs.getString(i));
				}
				model.addRow(r);
				cnt++;
			}

			rs.close();
			pstmt.close();
			searchsql.requestFocus();
		}catch(Exception ex){
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "잘못된 SQL문장이거나 테이블이 없습니다.", "경고", JOptionPane.WARNING_MESSAGE);
		}	
	}
}
